package org.robot.scm.platform.monitor;

import java.io.File;
import java.util.Date;
import java.util.Objects;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.platform.AbstractMonitor;
import org.robot.scm.pojos.BaseNotify;
import org.robot.scm.pojos.enums.PlatformEnum;
import org.robot.scm.pojos.platform.Day60SBaseNotify;
import org.robot.scm.retry.SimpleRetryable;
import org.robot.scm.utils.DateTool;
import org.robot.scm.utils.NetImageTool;

/**
 * 每天60s新闻
 */
@Slf4j
public class Day60sNoticeMonitor extends AbstractMonitor {

    @Override
    public PlatformEnum getPlatform() {
        return PlatformEnum.DAY60S;
    }

    @SneakyThrows
    @Override
    public BaseNotify doMonitor(BaseConfig config) {
        return new SimpleRetryable(3, 3000).retry(() -> this.read(config), Objects::nonNull);
    }

    private BaseNotify read(BaseConfig config) {
        File destFile = new File("assets/articles/60s.png");
        boolean download = NetImageTool.download(config.getCurrentPlatform().getNoticeSrc(), destFile.getAbsolutePath());
        if (!download) {
            return null;
        }
        return Day60SBaseNotify.builder()
                .url(destFile.getAbsolutePath())
                .createDay(DateTool.format(new Date(), DateTool.PATTERN_5))
                .build();
    }
}
